
//% color="#ff9f06" iconWidth=50 iconHeight=40
namespace ina219_edu{
  

    //% block="power meter [OBJ] initialization until successful, I2C address [ADDR], calibration value [OFFSET]" blockType="command"
    //% OBJ.shadow="normal" OBJ.defl=ina1
    //% ADDR.shadow="dropdown"  ADDR.options="ADDR"
    //% OFFSET.shadow="number" OFFSET.defl=0
    export function ina219Init(parameter: any, block: any) {
        var obj=parameter.OBJ.code;
        var addr=parameter.ADDR.code;
        var offset=parameter.OFFSET.code;
        Generator.addInclude("includeina219edu", `#include "DFRobot_INA219_EDU.h"`);
        Generator.addObject(`objina219${obj}`, `DFRobot_INA219_EDU_IIC`, `${obj}(&Wire, ${addr});`);
        Generator.addSetup(`setina219${obj}`, `${obj}.setOffset(${offset});`)
        Generator.addSetup(`idina219${obj}`, `while(${obj}.begin() != true) {delay(1000);}`)
        
        
    
    }

    //% block="power meter [OBJ] obtain no-load calibration value" blockType="command"
    //% OBJ.shadow="normal" OBJ.defl=ina1
    export function ina219ReadOffSet(parameter: any, block: any) {
        var obj=parameter.OBJ.code;
        Generator.addSetup(`ina219serialbegin${obj}`, `Serial.begin(9600);`)
        Generator.addCode(`Serial.println(${obj}.getOffset());`);
        
    }


    //% block="power meter [OBJ] read load [VAL]" blockType="reporter"    
    //% OBJ.shadow="normal" OBJ.defl=ina1
    //% VAL.shadow="dropdown"  VAL.options="VAL"
    export function ina219Get(parameter: any, block: any) {
        var obj=parameter.OBJ.code;
        var val=parameter.VAL.code;
        Generator.addCode(`${obj}.${val}()`);
        
    }

    function replaceQuotationMarks(str:string){
            str=str.replace(/"/g, ""); //去除所有引号
            return str
    }


    
}
